package us.mooresr.aircraftscheduling.client.ui;

import java.util.ArrayList;

import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.UseGatekeeper;

import us.mooresr.aircraftscheduling.client.DispatchCallback;
import us.mooresr.aircraftscheduling.client.LoggedInGatekeeper;
import us.mooresr.aircraftscheduling.client.events.ReservationDeletedEvent;
import us.mooresr.aircraftscheduling.client.place.NameTokens;
import us.mooresr.aircraftscheduling.shared.action.DeleteReservationAction;
import us.mooresr.aircraftscheduling.shared.action.DeleteReservationResult;
import us.mooresr.aircraftscheduling.shared.action.GetAllReservationsAction;
import us.mooresr.aircraftscheduling.shared.action.GetAllReservationsResult;
import us.mooresr.aircraftscheduling.shared.action.GetUserList;
import us.mooresr.aircraftscheduling.shared.action.GetUserListResult;
import us.mooresr.aircraftscheduling.shared.action.RetrieveAllOpenReservationsAction;
import us.mooresr.aircraftscheduling.shared.action.RetrieveAllOpenReservationsResult;
import us.mooresr.aircraftscheduling.shared.action.RetrieveReservation;
import us.mooresr.aircraftscheduling.shared.action.RetrieveReservationAction;
import us.mooresr.aircraftscheduling.shared.action.RetrieveReservationResult;
import us.mooresr.aircraftscheduling.shared.model.Reservation;

import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;

public class ReservationListPresenter extends
		Presenter<ReservationListPresenter.MyView, ReservationListPresenter.MyProxy> 
		implements ReservationListUiHandlers {

	public interface MyView extends View, HasUiHandlers<ReservationListUiHandlers> {
		void setData(ArrayList<Reservation> reservationList);
	}
	
	private final ReservationDetailPresenter detailDialog;
	private final DispatchAsync dispatcher;
	private final EventBus eventBus;

	@ProxyStandard
	@NameToken(NameTokens.reservationlist)
	@UseGatekeeper(LoggedInGatekeeper.class)
	public interface MyProxy extends ProxyPlace<ReservationListPresenter> {
	}

	@Inject
	public ReservationListPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy, final ReservationDetailPresenter detailDialog,
			DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		
		this.detailDialog = detailDialog;
		this.dispatcher = dispatcher;
		this.eventBus = eventBus;
		
		getView().setUiHandlers(this);
	}

	@Override
	protected void revealInParent() {
		RetrieveAllOpenReservationsAction action = new RetrieveAllOpenReservationsAction();
		
		dispatcher.execute(action, new AsyncCallback<RetrieveAllOpenReservationsResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(RetrieveAllOpenReservationsResult result) {
				
				getView().setData(result.getReservations());
				
				RevealContentEvent.fire(eventBus, MainPagePresenter.TYPE_SetMainContent, ReservationListPresenter.this);
				
			}
			
		});
		
		// RevealRootContentEvent.fire(this, this);
		
	}

	/**
	 * Request from the ReservationList to create a new Reservation.  Prepare and pop up the dialog.
	 */
	
	@Override
	public void createReservation() {
		// We first need to get the list of Users so that we can fill in the
		// ListBox on the dialog
		
		GetUserList action = new GetUserList();
		dispatcher.execute(action, new AsyncCallback<GetUserListResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GetUserListResult result) {
				detailDialog.clear();
				detailDialog.loadUserPopup(result.getUserList());
				addToPopupSlot(detailDialog);
				
			}

		});
		
		
		
	}

	@Override
	public void deleteReservation(Reservation reservation) {
		DeleteReservationAction action = new DeleteReservationAction(reservation);
		
		dispatcher.execute(action, new AsyncCallback<DeleteReservationResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(DeleteReservationResult result) {
				// fireEvent(new ReservationDeletedEvent(result.getReservationOut()));
				ReservationDeletedEvent.fire(getEventBus(), result.getReservationOut());
				
			}
			
		});
		
	}

	@Override
	public void editReservation(final Reservation reservation) {
		// We first need to get the list of Users so that we can fill in the
		// ListBox on the dialog
		
		GetUserList action = new GetUserList();
		dispatcher.execute(action, new AsyncCallback<GetUserListResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GetUserListResult result) {
				detailDialog.clear();
				detailDialog.loadUserPopup(result.getUserList());
				
				// Now retrieve the reservation to be edited
				RetrieveReservationAction action = new RetrieveReservationAction(reservation.getReservationId());
				dispatcher.execute(action, new AsyncCallback<RetrieveReservationResult>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(RetrieveReservationResult result) {
						detailDialog.setData(result.getReservation());
						addToPopupSlot(detailDialog);
						
					}
					
				});
				
				
			}

		});
		
	}

	@Override
	public void refreshList(Boolean showPast) {
		// The view wants its list refreshed
		if (showPast) {
			GetAllReservationsAction action = new GetAllReservationsAction();
			dispatcher.execute(action, new AsyncCallback<GetAllReservationsResult>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(GetAllReservationsResult result) {
					getView().setData(result.getReservations());
					
				}
				
			});
		} else {
			RetrieveAllOpenReservationsAction action = new RetrieveAllOpenReservationsAction();
			dispatcher.execute(action, new AsyncCallback<RetrieveAllOpenReservationsResult> () {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(RetrieveAllOpenReservationsResult result) {
					getView().setData(result.getReservations());
					
				}
			});
		}
		
	}
}
